﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace eSYSU.Web.Common
{
    /// <summary>
    /// 提供本地Session/Cookie等相关操作的功能
    /// </summary>
    public class LocalStorage
    {
        public LocalStorage()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }


        #region Session部分
        /// <summary>
        /// 为当前会话创建一个Session
        /// </summary>
        /// <param name="sSessionName">Session名称</param>
        /// <param name="i">过期时间</param>
        public static void SetSession(string sSessionName, string sSessionValue, int iTimeout)
        {
            HttpContext.Current.Session[sSessionName] = sSessionValue;
            HttpContext.Current.Session.Timeout = iTimeout;
        }


        /// <summary>
        /// 检查指定的Session是否有效
        /// </summary>
        /// <param name="sSessionName">Session名称</param>
        /// <returns>检查结果</returns>
        public static bool CheckSession(string sSessionName)
        {
            bool IsWork = false;

            if (HttpContext.Current.Session[sSessionName] != null)
            {
                IsWork = true;
            }

            return IsWork;
        }
        #endregion

        #region Cookie部分
        /// <summary>
        /// 创建一个多维Cookie，若该Cookie已存在，则更新之
        /// </summary>
        /// <param name="strToStore">Cookie名称</param>
        /// <param name="strToStore">Cookie的键名</param>
        /// <param name="intExpire">Cookie的键值</param>
        /// <param name="intExpire">Cookie的有效期限，以天为单位</param>
        /// <returns>创建结果</returns>
        public static bool SetCookie(string strCookieName, string strKey, string strValue, int intExpire)
        {
            try
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strCookieName];

                if (cookie == null)
                {
                    cookie = new HttpCookie(strCookieName);

                    cookie.Values.Add(strKey, strValue);
                    cookie.Expires = DateTime.Now.AddDays(intExpire);
                    HttpContext.Current.Response.AppendCookie(cookie);
                    //HttpContext.Current.Response.Cookies.Add(cookie);
                }
                else
                {
                    if (System.Web.HttpContext.Current.Request.Cookies[strCookieName].Values[strKey] != null)   //已有相应的键
                    {
                        cookie.Values.Set(strKey, strValue);
                    }
                    else
                    {
                        cookie.Values.Add(strKey, strValue);
                        HttpContext.Current.Response.AppendCookie(cookie);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        /// <summary>
        /// 检查指定名称的Cookie是否存在
        /// </summary>
        /// <param name="strCookieName">Cookie名称</param>
        /// <returns>搜索结果</returns>
        public static bool GetCookieValue(string strCookieName)
        {
            if (HttpContext.Current.Request.Cookies[strCookieName] != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 获取指定名称和键名的Cookie值
        /// </summary>
        /// <param name="strCookieName">Cookie名称</param>
        /// <param name="strCookieName">Cookie键名</param>
        /// <returns>Cookie键值，若指定的Cookie已失效，则返回值为"null"</returns>
        public static string GetCookieValue(string strCookieName,string strKey)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strCookieName];

            if (cookie != null)
            {
                if (cookie.Values[strKey] != null)
                    return cookie.Values[strKey];
                else
                    return "null";
            }
            else
            {
                return "null";
            }
        }
        #endregion
    }
}